{{/* Hugo Blox: People */}}
{{/* Documentation: https://hugoblox.com/blocks/ */}}
{{/* License: https://github.com/HugoBlox/hugo-blox-builder/blob/main/LICENSE.md */}}

{{/* Initialise */}}
{{ $page := .wcPage }}
{{ $block := .wcBlock }}
{{ $show_social := $block.design.show_social | default false }}
{{ $show_interests := $block.design.show_interests | default true }}
{{ $show_organizations := $block.design.show_organizations | default false }}
{{ $show_role := $block.design.show_role | default true }}

<div class="row justify-content-center people-widget">
  {{ with $block.content.title }}
    <div class="section-heading col-12 mb-3 text-center">
      <h1 class="mb-0">{{ . | markdownify | emojify }}</h1>
      {{ with $block.content.subtitle }}<p class="mt-1">{{ . | markdownify | emojify }}</p>{{ end }}
    </div>
  {{ end }}

  {{ with $block.content.text }}
    <div class="col-md-12">
      {{ . | emojify | $page.RenderString }}
    </div>
  {{ end }}

  {{ range $block.content.user_groups }}
    {{ $query := where (where site.Pages "Section" "authors") ".Params.user_groups" "intersect" (slice .) }}

    {{/* Sort */}}
    {{ $sort_by := $block.content.sort_by | default "Params.last_name" }}
    {{ $sort_by = partial "blox-core/functions/get_sort_by_parameter" $sort_by }}
    {{ $sort_ascending := $block.content.sort_ascending | default true }}
    {{ $sort_order := cond $sort_ascending "asc" "desc" }}
    {{ $query = sort $query $sort_by $sort_order }}

    {{if $query | and (gt (len $block.content.user_groups) 1) }}
      <div class="col-md-12">
        <h2 class="mb-4">{{ . | markdownify }}</h2>
      </div>
    {{end}}

    {{ range $query }}
      {{ $avatar := (.Resources.ByType "image").GetMatch "*avatar*" }}
      {{/* Get link to user's profile page. */}}
      {{ $link := "" }}
      {{ with site.GetPage (printf "/authors/%s" (path.Base .File.Dir)) }}
        {{ $link = .RelPermalink }}
      {{ end }}
      <div class="col-12 col-sm-auto people-person">
        {{ $src := "" }}
        {{ if site.Params.features.avatar.gravatar }}
          {{ $src = printf "https://s.gravatar.com/avatar/%s?s=150" (md5 .Params.email) }}
        {{ else if $avatar }}
          {{ $avatar_image := $avatar.Fill "270x270 Center" }}
          {{ $src = $avatar_image.RelPermalink }}
        {{ end }}
        {{ if $src }}
          {{ $avatar_shape := site.Params.features.avatar.shape | default "circle" }}
          {{with $link}}<a href="{{.}}">{{end}}<img width="270" height="270" loading="lazy" class="avatar {{if eq $avatar_shape "square"}}avatar-square{{else}}avatar-circle{{end}}" src="{{ $src }}" alt="Avatar">{{if $link}}</a>{{end}}
        {{ end }}

        <div class="portrait-title">
          <h2>{{with $link}}<a href="{{.}}">{{end}}{{ .Title }}{{if $link}}</a>{{end}}</h2>
          {{ if and $show_organizations .Params.organizations }}{{ range .Params.organizations }}<h3>{{ .name }}</h3>{{ end }}{{ end }}
          {{ if and $show_role .Params.role }}<h3>{{ .Params.role | markdownify | emojify }}</h3>{{ end }}
          {{ if $show_social }}{{ partial "social_links" . }}{{ end }}
          {{ if and $show_interests .Params.interests }}<p class="people-interests">{{ delimit .Params.interests ", " | markdownify | emojify }}</p>{{ end }}
        </div>
      </div>
    {{ end }}
  {{ end }}
</div>
